package com.zirenx.sys.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zirenx.annotation.RouteMapping;
import com.zirenx.common.controller.BaseController;
import com.zirenx.common.controller.IController;
import com.zirenx.sys.model.SysLog;
import com.zirenx.sys.service.SysLogService;
import com.zirenx.utils.LogUtils;
import com.zirenx.utils.R;
import com.zirenx.utils.RequestUtils;
import com.zirenx.utils.StringUtils;

@RouteMapping(value = "/sys/log")
public class SysLogController extends BaseController implements IController{

	@Inject
	SysLogService sysLogService;
	
	@Override
	public void index() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void add() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void save() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void edit() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void remove() {
		// TODO 单个删除
		R r = R.ok();
		try {
			String id = get("id");
			SysLog bean = new SysLog();
			bean.findById(id).delete();
		} catch (Exception e) {
			LogUtils.error(this.getClass(),e);
			r = R.error();
		}
		renderJson(r);
	}

	@Override
	public void dataAjax() {
		// TODO 数据列表
		R r = R.ok();
		try {
			SysLog bean = getBean(SysLog.class,"bean");
			
			SqlPara sqlPara = new SqlPara();
			String sql = "select * from " + bean.getTableName() + " bean where 1=1 ";

			sql += appendSearch(sqlPara);

			sql+=" order by bean.id desc";
			sqlPara.setSql(sql);
			Page<SysLog> p = bean.paginate(getPageNumber(), getPageSize(), sqlPara);
			List<SysLog> list = p.getList();

			r.put("rows", list);
			r.put("total", p.getTotalRow());
		} catch (Exception e) {
			LogUtils.error(this.getClass(),e);
			r = R.error(); 
		}
		renderJson(r);
	}

	@Override
	public void batchRemove() {
		// TODO 批量删除
		R r = R.ok();
		try {
			String[] ids = getParaValues("ids");
			if (ids!=null && ids.length>0){
				SysLog bean = new SysLog();
				for (String id : ids){
					bean.findById(id).delete();
				}
			}
		} catch (Exception e) {
			LogUtils.error(this.getClass(),e);
			r = R.error();
		}
		renderJson(r);
	}

	public void queryIp(){
		R r = R.ok();
		try {
			String id = get("id");
			if (!StringUtils.isEmpty(id)){
				SysLog bean = new SysLog().findById(id);
				if (bean!=null){
					String ip = bean.getIp();
					String str = "";
					if (ip.indexOf("0:0:0") > -1 || ip.indexOf("127.0.0.1") > -1){
						str = "本地";
					}else if (ip.indexOf("192.168") > -1){
						str = "内网";
					}else {
						RequestUtils requestUtils = new RequestUtils();
						String doGet = requestUtils.doGet("http://ip.taobao.com/service/getIpInfo.php?ip="+ip);

						Log.getLog(this.getClass()).info(doGet);

						JSONObject jsonObject = JSONObject.parseObject(doGet);
						if ("0".equals(jsonObject.get("code").toString())){
							JSONObject data = JSONObject.parseObject(jsonObject.get("data").toString());
							str = data.get("country").toString()
									+ data.get("region").toString()
									+ data.get("city").toString()
									+ data.get("isp").toString();
							r.put("data",str);
						}
					}
					bean.setIpRegion(str);
					bean.update();
				}
			}
		} catch (Exception e) {
			LogUtils.error(this.getClass(),e);
			r = R.error();
		}
		renderJson(r);
	}
}
